<?php

class Admin_LoaiVanBanController extends Zend_Controller_Action
{
	public function init()
	{
		$this->_helper->layout->setLayout('admin');
	}
	
	public function indexAction()
	{
		$loaivanbanMapper = Library_Model_LoaiVanBanMapper::getInstance();
		$loaivanban = $loaivanbanMapper->loaivanbanList();
		$this->view->loaivanban = $loaivanban;
		
		
	}
	
	public function themAction()
	{
		$this->view->title = 'Thêm mới Nhóm văn bản:';
		$formThem = new Admin_Form_LoaiVanBanForm();
		
		$formThem->setMethod('post');
		$formThem->setAction('/zfMHB/public/admin/loaivanban/them');
		
		$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
		$this->view->form = $formThem;
		
		$formThem->getElement('tenloai')->addValidator('Db_NoRecordExists',true, array(
				'table' => 'tbl_loaivanban',
				'field' => 'TenLoai'
		));
		$formThem->getElement('tenloai')->getValidator('Db_NoRecordExists')->setMessage('Loại VB đã tồn tại!');
		
		if($this->getRequest()->isPOST())
		{
			if($formThem->isValid($_POST))
			{
				$data = $formThem->getValues();
				$tenloai = $data['tenloai'];
				$loaivanbanMapper = Library_Model_LoaiVanBanMapper::getInstance();
				
				$loaivanbanObj = new Library_Model_LoaiVanBan();
				$loaivanbanObj->setTenLoai($tenloai);
				
				$loaivanban = $loaivanbanMapper->save($loaivanbanObj);
				$this->_redirect('/admin/loaivanban/index');
			}
		}
	}
	
	public function capnhatAction()
    {
        $this->view->title = 'Cập nhật loại VB: ';
        $formCapnhat = new Admin_Form_LoaiVanBanForm();        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/loaivanban/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $maloaivanban = $this->getRequest()->getParam('id');

        $formCapnhat->getElement('tenloai')->addValidator('Db_NoRecordExists', true, array(
        		'table' => 'tbl_loaivanban',
        		'field' => 'TenLoai',
        		'exclude' => array(
        				'field' => 'MaLoaiVanBan',
        				'value' => $maloaivanban)
        ));
        
     	$formCapnhat->getElement('loaivanban')->getValidator('Db_NoRecordExists')->setMessage('Loại VB đã tồn tại!');
        $loaivanbanMapper = Library_Model_LoaiVanBanMapper::getInstance();
        $this->view->form = $formCapnhat;
        
       if($this->getRequest()->isPOST())
		{
			if($formCapnhat->isValid($_POST))
			{
				$data = $formCapnhat->getValues();
				
				$id =	$data['id'];
				$tenloai = $data['loaivanban'];
				
				$loaivanbanObj = new Library_Model_LoaiVanBan();
				
				$loaivanbanObj->setId($id);
				$loaivanbanObj->setTenLoai($tenloai);
				
				$loaivanban = $loaivanbanMapper->save($loaivanbanObj);
				$this->_redirect('/admin/loaivanban/index');
				
			}
		}
		else
		{
			$loaivanban = $loaivanbanMapper->searchLoaiVanBanById($maloaivanban);

			$formCapnhat->getElement('id')->setValue($loaivanban->getID());
			$formCapnhat->getElement('loaivanban')->setValue($loaivanban->getTenloai());
		}
	}
	
	public function xoaAction()
	{
		$this->_helper->viewRenderer->setNoRender(true);
		$maloaivanban = $this->getRequest()->getParam('id');
		$loaivanbanMapper = Library_Model_LoaiVanBanMapper::getInstance();
		$loaivanbanMapper->delete($maloaivanban);
		$this->_redirect('/admin/loaivanban/index');
		
	}
}